package edu.ynu.se.xiecheng.achitectureclass.service;


import edu.ynu.se.xiecheng.achitectureclass.common.service.LogicService;
import edu.ynu.se.xiecheng.achitectureclass.domain.Result;
import edu.ynu.se.xiecheng.achitectureclass.dao.BusinessDao;
import edu.ynu.se.xiecheng.achitectureclass.domain.vo.BusinessVo;
import edu.ynu.se.xiecheng.achitectureclass.domain.entity.Business;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class BusinessService extends LogicService<BusinessDao, Business,Long> {
    @Resource
    BusinessDao businessDao;
    public BusinessService(@Autowired BusinessDao lr) {
        super(lr);
    }

    public Result getAllBusiness(int size, int page){
        Pageable pageable = PageRequest.of(page,size, Sort.by("id").descending());
        Page<Business> all = getDAO().findAll(pageable);
        List<Business> content = all.getContent();
        List<BusinessVo> newList = new ArrayList<>();

        for (int i = 0; i <content.size() ; i++) {
            BusinessVo businessVo = new BusinessVo();
            BeanUtils.copyProperties(content.get(i), businessVo);
            newList.add(businessVo);
        }

        return new Result(true,newList);
    }
}
